package com.moli.iov.admin.config;

import com.moli.iov.admin.web.interceptor.AuthInterceptor;
import com.moli.iov.admin.web.interceptor.LoginInterceptor;
import javax.servlet.MultipartConfigElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * webmvc配置
 * </p>
 *
 * @author zql
 * @since 2019/9/20 10:38
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

  @Autowired
  private LoginInterceptor loginInterceptor;

  @Autowired
  private AuthInterceptor authInterceptor;

  /**
   * 拦截器需要排除的api
   */
  private static final String[] EXCLUDE_PATH =
      {"/auth/user/login", "/auth/user/logout", "/auth/user/platform/updatePwd", "/socketjs/**","/websocket/**"};

  /**
   * 注册拦截器
   * @param registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(loginInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns(EXCLUDE_PATH);

    registry.addInterceptor(authInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns(EXCLUDE_PATH);
  }
  /**
   * 配置上传文件大小的配置
   * @return
   */
  @Bean
  public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    //  单个数据大小
    factory.setMaxFileSize("102400KB");
    /// 总上传数据大小
    factory.setMaxRequestSize("102400KB");
    return factory.createMultipartConfig();
  }
}
